package com.syg.transaction.service;

import com.syg.transaction.dao.entity.TClass;
import com.syg.transaction.dao.mapper.TClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.UUID;

/**
 * 在日志中启用事务日志
 * logging.level.org.springframework.transaction.interceptor = TRACE
 * <p>
 * 手动回滚事务
 * TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 * <p>
 * 获取事务状态
 * boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
 *
 * @author syg
 */
@Service
public class TClassService {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    TClassMapper tClassMapper;

    /**
     * 添加元素
     *
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean add1() {
        add2();
        try {
            TClassService bean = applicationContext.getBean(TClassService.class);
            bean.add2();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public void add() {
        TClass tClass = new TClass();
        tClass.setId(UUID.randomUUID().toString().substring(0, 32));
        tClass.setClassName("超级一班");
        tClass.setClassNo("369");
        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
        System.out.println("add1是否在事务中," + actualTransactionActive + ",事务名=" + TransactionSynchronizationManager.getCurrentTransactionName());


        boolean currentTransactionReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
        System.out.println("add1是否是只读事务," + currentTransactionReadOnly);
        // 返回值为空,也会报空指针异常
        tClassMapper.add(tClass);
    }

    /**
     * 添加元素
     *
     * @return
     */
    public boolean add2() {
        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
        System.out.println("add2是否在事务中," + actualTransactionActive + ",事务名=" + TransactionSynchronizationManager.getCurrentTransactionName());

        TransactionStatus transactionStatus = TransactionAspectSupport.currentTransactionStatus();
        System.out.println("add2是否仅仅回滚" + transactionStatus.isRollbackOnly() + ",是否是新事务," + transactionStatus.isNewTransaction() + ",是否有还原点," + transactionStatus.hasSavepoint());

        boolean currentTransactionReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
        System.out.println("add2是否是只读事务," + currentTransactionReadOnly);
        TClass tClass = new TClass();
        tClass.setId(UUID.randomUUID().toString().substring(0,32));
        tClass.setClassName("超级2班");
        tClass.setClassNo("123");
        // 返回值为空,也会报空指针异常
        tClassMapper.add(tClass);
        throw new RuntimeException();
    }

}

